import pyautogui as pg
import os
import time
import pyperclip
import requests
import json
import psutil
import sys
import win32gui, win32com.client, win32api
import win32con
import win32process  
import ctypes
from pywinauto.application import Application




file_path = 'C:\\Users\\libokai\\Desktop\\1896902747711878.Qdy4'

def set_foreground_window(hwnd):  
    _, pid = win32process.GetWindowThreadProcessId(hwnd)  
    ctypes.windll.user32.AllowSetForegroundWindow(pid)  
    shell = win32com.client.Dispatch("WScript.Shell")
    shell.SendKeys('%{1}')
    win32gui.SetForegroundWindow(hwnd)  

def enum_child_windows(hwnd, result):  
    result.append((hwnd, win32gui.GetWindowText(hwnd)))  

hwnd = win32gui.FindWindow(None, "新建向导")
# hwnd2 = win32gui.FindWindow(None, "新建建设项目")
# print(hwnd2)

app = Application(backend='uia').connect(handle=hwnd)
dlg = app.window(title=win32gui.GetWindowText(hwnd))  # 根据你的窗口标题进行调整  

 # 获取屏幕分辨率  
screen_width = win32api.GetSystemMetrics(win32con.SM_CXSCREEN)  
screen_height = win32api.GetSystemMetrics(win32con.SM_CYSCREEN)  

# 获取窗口当前位置和大小  
left, top, right, bottom = win32gui.GetWindowRect(hwnd)  
window_width = right - left  
window_height = bottom - top  

# 计算新位置  
new_left = (screen_width - window_width) // 2  
new_top = (screen_height - window_height) // 2  

# 移动窗口  
win32gui.SetWindowPos(hwnd, win32con.HWND_TOP, new_left, new_top, window_width, window_height, 0)

left, top, right, bottom = win32gui.GetWindowRect(hwnd) 


print(str(left) + " " + str(top) + " " + str(right) + " " + str(bottom))

print((right - left) * (1 * (1.75 / 5)) + left, (bottom - top) * ((1.0 / 6) * 1) + top)


pg.click((right - left) * (1 * (1.75 / 5)) + left, (bottom - top) * ((1.0 / 6) * 1) + top)



time.sleep(100)

# pg.typewrite("测试建设项目")
time.sleep(1)


new_construct = win32gui.FindWindow(None, "新建建设项目文件")
child_windows = []  
win32gui.EnumChildWindows(new_construct, enum_child_windows, child_windows)  
# print(len(child_windows))
# 假设我们找到了一个类名为 'EDIT' 的子窗口（通常编辑框的类名）  
edit_hwnd = None  
confirm_hwnd = None
for hwnd_child, window_text_child in child_windows:  
    # print(str(hwnd_child) + " " + win32gui.GetClassName(hwnd_child) + " " + win32gui.GetWindowText(hwnd_child))
    # print(win32gui.GetClassName(hwnd_child) + " " + win32gui.GetWindowText(hwnd_child))
    if win32gui.GetClassName(hwnd_child) == 'TEdit':  
        edit_hwnd = hwnd_child 
win32gui.SendMessage(edit_hwnd, win32con.WM_SETTEXT, 0, "新建建设项目文件1")
pg.hotkey("enter")
pg.hotkey("enter")
pg.hotkey("enter")
pg.hotkey("enter")
pg.hotkey("enter")
pg.hotkey("enter")
pg.hotkey("enter")
pg.hotkey("enter")
time.sleep(1)
pg.hotkey("enter")
time.sleep(5)
pg.hotkey("tab")
pg.hotkey("tab")
pg.hotkey("shift", "f10")
pg.hotkey("e")


pg.hotkey("shift", "f10")
pg.hotkey("s")
open = win32gui.FindWindow(None, "打开")
if open:
    # win32gui.ShowWindow(hwnd, win32con.SW_MAXIMIZE)
    print(f"打开窗口句柄为：{open}")
    time.sleep(1)
else:
    print("打开未找到指定窗口")
child_windows = []  
win32gui.EnumChildWindows(open, enum_child_windows, child_windows)  
# print(len(child_windows))
# 假设我们找到了一个类名为 'EDIT' 的子窗口（通常编辑框的类名）  
edit_hwnd = None  
confirm_hwnd = None
for hwnd_child, window_text_child in child_windows:  
    # print(str(hwnd_child) + " " + win32gui.GetClassName(hwnd_child) + " " + win32gui.GetWindowText(hwnd_child))
    if win32gui.GetClassName(hwnd_child) == 'Edit':  
        edit_hwnd = hwnd_child  
win32gui.SendMessage(edit_hwnd, win32con.WM_SETTEXT, 0, file_path)
set_foreground_window(open)
# win32gui.SetFocus(open)
pg.hotkey("Enter")




